<?php

include_once $REP_OUTILS . 'Selector.php';
include_once $REP_OUTILS . 'MyException.php';
include_once $REP_OUTILS . 'Outils.php';
// Pour FirePHP
include_once($REP_LIB . 'FirePHPCore/FirePHP.class.php');


/**
 * PariGest - class.Compte.php
 *
 * $Id$
 *
 * This file is part of PariGest.
 *
 * Automatically generated on 06.08.2011, 21:02:14 with ArgoUML PHP module 
 * (last revised $Date: 2010-01-12 20:14:42 +0100 (Tue, 12 Jan 2010) $)
 *
 * @author firstname and lastname of author, <author@example.org>
 */

if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

/**
 * include Operation
 *
 * @author firstname and lastname of author, <author@example.org>
 */
require_once('class.Operation.php');

/**
 * include Parametrage
 *
 * @author firstname and lastname of author, <author@example.org>
 */
require_once('class.Parametrage.php');

/**
 * include Pari
 *
 * @author Séb
 */
require_once('class.Pari.php');

/* user defined includes */
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000868-includes begin
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000868-includes end

/* user defined constants */
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000868-constants begin
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000868-constants end

/**
 * Short description of class Compte
 *
 * @access public
 * @author firstname and lastname of author, <author@example.org>
 */
class Compte
{
    // --- ASSOCIATIONS ---
    // generateAssociationEnd :     // generateAssociationEnd :     // generateAssociationEnd :     // generateAssociationEnd : 

    // --- ATTRIBUTES ---

    /**
     * Short description of attribute IdCompte
     *
     * @access private
     * @var integer
     */
    private $IdCompte = null;

    /**
     * Short description of attribute Solde
     *
     * @access private
     * @var UnlimitedInteger
     */
    private $Solde = null;
    
    /**
    * Short description of attribute Solde
    *
    * @access private
    * @var UnlimitedInteger
    */
    //private $SoldeVirtuel = null;

    /**
     * Short description of attribute DateMaj
     *
     * @access private
     * @var Date
     */
    private $DateMaj = null;

    /**
     * Short description of attribute Commentaire
     *
     * @access private
     * @var String
     */
    private $Commentaire = null;

    /**
     * Short description of attribute Libelle
     *
     * @access public
     * @var String
     */
    public $Libelle = null;
    /**
    * Short description of attribute Defaut
    *
    * @access public
    * @var Integer
    */
    public $Defaut = null;
    
    /** Nom de l'IdCompte en session */
    public static $ID_COMPTE_SESSION = 'PARIGEST_IDCOMPTE';

    // --- OPERATIONS ---

    function __construct($db)
    {
      // Création de la connexion
      $this->maConnexion = $db;
      // Initialisation des requêtes
      $this->initRequete();
      // Préparation des requêtes
      $this->prepare();
      $this->trace = FirePHP::getInstance(true);
    }
    
    /**
     * Initialisation des requêtes préparées
     */
    private function initRequete(){
    
      $this->sqlInsert = "INSERT INTO Compte (Solde, DateMaj, Commentaire, Libelle, Defaut) VALUES(?, ?, ?, ?, ?)";
      $this->sqlDelete = "DELETE FROM Compte WHERE `IdCompte`=?";
      $this->sqlUpdate = "UPDATE Compte SET Solde = ?, DateMaj = ?, Commentaire = ?, Libelle = ?, Defaut = ? WHERE `IdCompte` = ?";
    
    }
    /**
     * Préparation des requêtes
     */
    private function prepare()
    {
      $this->reqInsert       = $this->maConnexion->prepare($this->sqlInsert);
      $this->reqDelete       = $this->maConnexion->prepare($this->sqlDelete);
      $this->reqUpdate       = $this->maConnexion->prepare($this->sqlUpdate);
    
    }
    
    /**
     * Short description of method affichage
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function affichage()
    {
      $this->trace->fb('Compte->affichage() : ' . $this->toStringClePrimaire());
      $user = new Compte($this->maConnexion);
      $user->setIdCompte($this->getIdCompte());
      // Lecture de la première ligne
      $stmt = $user->select();
      $result = $stmt->fetch();

      $this->IdCompte      = $result['IdCompte'];
      $this->Solde         = $result['Solde'];
      $this->DateMaj       = $result['DateMaj'];
      $this->Libelle       = $result['Libelle'];
      $this->Commentaire   = $result['Commentaire'];
//       $this->SoldeVirtuel  = $result['SoldeVirtuel'];
      $this->Defaut        = $result['Defaut'];

      // Fermeture du curseur
      $stmt->closeCursor();
    }

    public function toStringClePrimaire()
    {
      return '|' . $this->IdCompte . '|';
    }
    
    /**
     * Short description of method modification
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function modification()
    {
      $this->trace->fb('Compte.modification : ' . $this->toStringClePrimaire());
      // Requete de modification
      $champs = array($this->Solde, getDateToday(), $this->Commentaire, $this->Libelle, $this->Defaut, $this->IdCompte);
      //print_r($champs);
      try
      {
        $this->trace->fb('avant modif');
        $retour =  $this->reqUpdate->execute($champs);
        $this->trace->fb('après modif');
        return $retour;
      }
      catch(Exception $e)
      {
        $this->trace->fb('Erreur : ' . $this->reqUpdate->errorInfo());
        //echo 'Erreur : <br>'; print_r($this->reqUpdate->errorInfo());
        throw new MyException('Une erreur est survenue lors de la modification du compte.', $this->reqUpdate->errorInfo());
      }      
    }

    /**
     * Short description of method suppression
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function suppression()
    {
      $this->trace->fb('suppression : ' . $this->toStringClePrimaire());
      // Requete de suppression
      try
      {
          $this->reqDelete->execute(array($this->IdCompte));
          $this->trace->fb('suppression réussie');          
      }
      catch(Exception $e)
      {
         //echo 'Erreur : <br>'; print_r($this->reqDelete->errorInfo());
         //throw new Exception('Une erreur est survenue lors de la suppression dans classe. Cause : ' . $e->getMessage() );
         throw new MyException('Une erreur est survenue lors de la suppression du compte. ', $this->reqDelete->errorInfo());
         
      }
    }

    /**
     * Short description of method insertion
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function insertion()
    {
    
      $this->trace->fb('insertion : ' . $this->toStringClePrimaire());
      // Requete d'insertion
      $champs = array(/*$this->getNextId(), */$this->Solde, $this->DateMaj, $this->Commentaire, $this->Libelle, $this->Defaut);

      try
      {
         $this->reqInsert->execute($champs);
         $this->trace->fb('insertion : ' . $this->maConnexion->lastInsertId());
         return $this->maConnexion->lastInsertId();
      }
      catch(Exception $e)
      {
         //echo 'Erreur : <br>'; print_r($this->reqInsert->errorInfo());
         throw new MyException('Une erreur est survenue lors de l\'insertion du compte.', $this->reqInsert->errorInfo());
      }
    }

    /**
    * Short description of method getNextId
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return Integer
    */
    /*public function getNextId()
    {
      $select = new Selector();

      // Les Champs
      $select->addChamp('IFNULL(MAX(IdCompte), 0)+1');

      // La Table
      $select->addFrom('Compte');

      // Les Critères
      //$select->addCritere('IdCompte', $this->IdCompte);
      
      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      $stmt = $this->maConnexion->query($select->toString());
      $row = $stmt->fetch();
      //$this->trace->fb('Résultat : ' .  $row[0]);
      return $row[0];
    }*/
    
    /**
     * Short description of method select
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     */
    public function select()
    {
      $this->trace->fb('Compte.select');
      $select = new Selector();
      $champs = array("IdCompte"=>$this->IdCompte, "Solde"=>$this->Solde, "DateMaj"=>$this->DateMaj, "Commentaire"=>$this->Commentaire, "Libelle"=>$this->Libelle, "Defaut"=>$this->Defaut);
      
      foreach($champs as $cle=>$valeur) 
      {
        // Les Champs
        $select->addChamp($cle);
        // Les Critères
        if( !empty($valeur) )
         $select->addCritere($cle, $valeur);
      }
      
      // La Table
      $select->addFrom('Compte');
      $this->trace->fb('Requete : ' . $select->toString());
      //echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }
    
   
    /**
     * Short description of method getIdCompte
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return Integer
     */
    public function getIdCompte()
    {
        return $this->IdCompte;
    }

    /**
     * Short description of method getSolde
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return UnlimitedInteger
     */
    public function getSolde()
    {
        return $this->Solde;
    }

    /**
     * Short description of method getDateMaj
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getDateMaj()
    {
        return $this->DateMaj;
    }

    /**
     * Short description of method getCommentaire
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return String
     */
    public function getCommentaire()
    {
        return $this->Commentaire;
    }

    /**
     * Short description of method setIdCompte
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  integer iIdCompteP
     * @return mixed
     */
    public function setIdCompte( $iIdCompteP )
    {
        $this->IdCompte = $iIdCompteP;
    }

    /**
     * Short description of method setSolde
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  UnlimitedInteger dSoldeP
     * @return mixed
     */
    public function setSolde( $dSoldeP)
    {
        $this->Solde = $dSoldeP;
    }

    /**
     * Short description of method setDateMaj
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  dDateMajP
     * @return mixed
     */
    public function setDateMaj($dDateMajP)
    {
        $this->DateMaj = $dDateMajP;
    }

    /**
     * Short description of method setCommentaire
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  String sCommentaireP
     * @return mixed
     */
    public function setCommentaire( $sCommentaireP)
    {
        $this->Commentaire = $sCommentaireP;
    }

 
    /**
     * Short description of method setLibelle
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setLibelle( $sLibelleP)
    {
        $this->Libelle = $sLibelleP;
    }

    /**
     * Short description of method getLibelle
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getLibelle()
    {
        return $this->Libelle;
    }

    /**
    * Short description of method setSoldeVirtuel
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return mixed
    */
    /*public function setSoldeVirtuel( $sSoldeVirtuelP)
    {
      $this->SoldeVirtuel = $sSoldeVirtuelP;
    }*/
    
    /**
     * Short description of method getSoldeVirtuel
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    /*public function getSoldeVirtuel()
    {
      //return $this->SoldeVirtuel;
      return $this->selectSoldeVirtuel();
    }
    */
    /*public function selectSoldeVirtuel()
    {
      
     
      $this->trace->fb('Compte.selectSoldeVirtuel()');
      
      $sousSelect = new Selector();
      $sousSelect->addChamp("MAX(idOperation)");
      $sousSelect->addFrom('Pari');
      $sousSelect->addCritere('Pari.IndResultat', Pari::$IND_RESULTAT_GAGNE);
      $sousSelect->addCritere('IdCompte', $this->IdCompte);
      
      $select = new Selector();
      
      // Les Champs
      $select->addChamp("IFNULL(SUM(Operation.montant), 0)");
      
      // La Table
      $select->addFrom('Pari');
      $select->addFrom('Operation');
      
      // Les Critères
      $select->addCritereSpecif('Pari.IdOperation >  (' . $sousSelect->toStringSsRequete(). ')');
      $select->addCritere('Pari.IdCompte', $this->IdCompte);
      $select->addCritereSpecif('Pari.idCompte = Operation.idCompte');
      $select->addCritereSpecif('Pari.idOperation = Operation.idOperation');
      
      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      $stmt = $this->maConnexion->query($select->toString());
      $result = $stmt->fetch();
      
      return $result[0];
    }
    */
    
    /**
    * Short description of method setDefaut
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return mixed
    */
    public function setDefaut( $iDefautP)
    {
      if( $iDefautP == '' )
      {
        setDefaut( '0' );
      }
      else
      {
        $this->Defaut = $iDefautP;
      }
    }
    
    /**
     * Short description of method getDefaut
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getDefaut()
    {
      return $this->Defaut;
    }
} /* end of class Compte */

?>